home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htswizard.c < prev    next >
C/C++ Source or Header  |  2006-08-15  |  37KB  |  1,042 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       wizard system (accept/refuse links)                    */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscore.h"
  42. #include "htswizard.h"
  43.  
  44. /* specific definitions */
  45. #include "htsbase.h"
  46. #include <ctype.h>
  47. /* END specific definitions */
  48.  
  49. // version 1 pour httpmirror
  50. // flusher si on doit lire peu α peu le fichier
  51. #define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); }
  52.  
  53. // pour allΘger la syntaxe, des raccourcis sont crΘΘs
  54. #define urladr   (liens[ptr]->adr)
  55. #define urlfil   (liens[ptr]->fil)
  56.  
  57. // libΘrer filters[0] pour insΘrer un ΘlΘment dans filters[0]
  58. #define HT_INSERT_FILTERS0 do {\
  59.   int i;\
  60.   if (*opt->filters.filptr > 0) {\
  61.     for(i = (*opt->filters.filptr)-1 ; i>=0 ; i--) {\
  62.       strcpybuff((*opt->filters.filters)[i+1],(*opt->filters.filters)[i]);\
  63.     }\
  64.   }\
  65.   (*opt->filters.filters)[0][0]='\0';\
  66.   (*opt->filters.filptr)++;\
  67.   assertf((*opt->filters.filptr) < opt->maxfilter); \
  68. } while(0)
  69.  
  70. typedef struct htspair_t {
  71.     char *tag;
  72.     char *attr;
  73. } htspair_t;
  74.  
  75. /* "embedded" */
  76. htspair_t hts_detect_embed[] = {
  77.     { "img", "src" },
  78.     { "link", "href" },
  79.  
  80.     /* embedded script hack */
  81.     { "script", ".src" },
  82.  
  83.     /* style */
  84.     { "style", "import" },
  85.  
  86.     { NULL, NULL }
  87. };
  88.  
  89. /* Internal */
  90. static int hts_acceptlink_(httrackp* opt,
  91.                                                     int ptr,int lien_tot,lien_url** liens,
  92.                                                     char* adr,char* fil,
  93.                                                     char* tag, char* attribute,
  94.                                                     int* set_prio_to,
  95.                                                     int* just_test_it);
  96.  
  97. /*
  98. httrackp opt     bloc d'options
  99. int ptr,int lien_tot,lien_url** liens
  100.                              relatif aux liens
  101. char* adr,char* fil
  102.                              adresse/fichier α tester
  103. char** filters,int filptr,int filter_max
  104.                              relatif aux filtres
  105. robots_wizard* robots
  106.                              relatif aux robots
  107. int* set_prio_to
  108.                              callback obligatoire "capturer ce lien avec prio=N-1"
  109. int* just_test_it
  110.                              callback optionnel "ne faire que tester ce lien Θventuellement"
  111. retour:
  112. 0 acceptΘ
  113. 1 refusΘ
  114. -1 pas d'avis
  115. */
  116.  
  117. int hts_acceptlink(httrackp* opt,
  118.                                      int ptr,int lien_tot,lien_url** liens,
  119.                                      char* adr,char* fil,
  120.                                      char* tag, char* attribute,
  121.                                      int* set_prio_to,
  122.                                      int* just_test_it) 
  123. {
  124.     int forbidden_url = hts_acceptlink_(opt, ptr, lien_tot, liens,
  125.         adr, fil, tag, attribute, set_prio_to, just_test_it);
  126.     int prev_prio = set_prio_to ? *set_prio_to : 0;
  127.  
  128.     // -------------------- PHASE 6 --------------------
  129.     {
  130.         int test_url = RUN_CALLBACK3(opt, check_link, adr, fil, forbidden_url);
  131.         if (test_url != -1) {
  132.             forbidden_url = test_url;
  133.             if (set_prio_to)
  134.                 *set_prio_to = prev_prio;
  135.         }
  136.     }
  137.  
  138.     return forbidden_url;
  139. }
  140.  
  141. static int cmp_token(const char *tag, const char *cmp) {
  142.     int p;
  143.     return (strncasecmp(tag, cmp, ( p = (int) strlen(cmp) ) ) == 0
  144.         && !isalnum((unsigned char) tag[p]));
  145. }
  146.  
  147. static int hts_acceptlink_(httrackp* opt,
  148.                                                     int ptr,int lien_tot,lien_url** liens,
  149.                                                     char* adr,char* fil,
  150.                                                     char* tag, char* attribute,
  151.                                                     int* set_prio_to,
  152.                                                     int* just_test_it) 
  153. {
  154.   int forbidden_url=-1;
  155.   int meme_adresse;
  156.     int embedded_triggered = 0;
  157. #define _FILTERS     (*opt->filters.filters)
  158. #define _FILTERS_PTR (opt->filters.filptr)
  159. #define _ROBOTS      ((robots_wizard*)opt->robotsptr)
  160.   int may_set_prio_to=0;
  161.  
  162.   // -------------------- PHASE 0 --------------------
  163.  
  164.   /* Infos */
  165.   if ((opt->debug>1) && (opt->log!=NULL)) {
  166.     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"wizard test begins: %s%s"LF,adr,fil);
  167.     test_flush;
  168.   }
  169.   
  170.   /* Already exists? Then, we know that we knew that this link had to be known */
  171.   if (adr[0] != '\0'
  172.     && fil[0] != '\0'
  173.     && opt->hash != NULL
  174.     && hash_read(opt->hash, adr, fil, 1, opt->urlhack) >= 0
  175.     ) {
  176.     return 0;  /* Yokai */
  177.   }
  178.   
  179.   // -------------------- PRELUDE OF PHASE 3-BIS --------------------
  180.  
  181.     /* Built-in known tags (<img src=..>, ..) */
  182.     if (forbidden_url != 0 && opt->nearlink && tag != NULL && attribute != NULL) {
  183.         int i;
  184.         for(i = 0 ; hts_detect_embed[i].tag != NULL ; i++) {
  185.             if (cmp_token(tag, hts_detect_embed[i].tag)
  186.                 && cmp_token(attribute, hts_detect_embed[i].attr)
  187.                 ) 
  188.             {
  189.                 embedded_triggered = 1;
  190.                 break;
  191.             }
  192.         }
  193.     }
  194.  
  195.  
  196.   // -------------------- PHASE 1 --------------------
  197.  
  198.   /* Doit-on traiter les non html? */
  199.   if ((opt->getmode & 2)==0) {    // non on ne doit pas
  200.     if (!ishtml(opt,fil)) {  // non il ne faut pas
  201.       //adr[0]='\0';    // ne pas traiter ce lien, pas traiter
  202.       forbidden_url=1;    // interdire rΘcupΘration du lien
  203.       if ((opt->debug>1) && (opt->log!=NULL)) {
  204.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"non-html file ignored at %s : %s"LF,adr,fil);
  205.         test_flush;
  206.       }
  207.       
  208.     }
  209.   }
  210.   
  211.   /* Niveau 1: ne pas parser suivant! */
  212.   if (ptr>0) {
  213.     if ( ( liens[ptr]->depth <= 0 ) || ( liens[ptr]->depth <= 1 && !embedded_triggered ) ) {
  214.       forbidden_url=1;    // interdire rΘcupΘration du lien
  215.       if ((opt->debug>1) && (opt->log!=NULL)) {
  216.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"file from too far level ignored at %s : %s"LF,adr,fil);
  217.         test_flush;
  218.       }
  219.     }
  220.   }
  221.  
  222.   /* en cas d'Θchec en phase 1, retour immΘdiat! */
  223.   if (forbidden_url == 1) {
  224.     return forbidden_url;
  225.   }
  226.   
  227.   // -------------------- PHASE 2 --------------------
  228.  
  229.   // ------------------------------------------------------
  230.   // doit-on traiter ce lien?.. vΘrifier droits de dΘplacement
  231.   meme_adresse=strfield2(adr,urladr);
  232.   if ((opt->debug>1) && (opt->log!=NULL)) {
  233.     HTS_LOG(opt,LOG_DEBUG); 
  234.     if (meme_adresse) 
  235.       fprintf(opt->log,"Compare addresses: %s=%s"LF,adr,urladr);
  236.     else
  237.       fprintf(opt->log,"Compare addresses: %s!=%s"LF,adr,urladr);
  238.     test_flush;
  239.   }
  240.   if (meme_adresse) {  // mΩme adresse 
  241.     {  // tester interdiction de descendre
  242.       // MODIFIE : en cas de remontΘe puis de redescente, il se pouvait qu'on ne puisse pas atteindre certains fichiers
  243.       // problΦme: si un fichier est virtuellement accessible via une page mais dont le lien est sur une autre *uniquement*..
  244.       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  245.       char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  246.       tempo[0] = tempo2[0] = '\0';
  247.       
  248.       // note (up/down): on calcule α partir du lien primaire, ET du lien prΘcΘdent.
  249.       // ex: si on descend 2 fois on peut remonter 1 fois
  250.       
  251.       if (lienrelatif(tempo,fil,liens[liens[ptr]->premier]->fil)==0) {
  252.         if (lienrelatif(tempo2,fil,liens[ptr]->fil)==0) {
  253.           if ((opt->debug>1) && (opt->log!=NULL)) {
  254.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"build relative links to test: %s %s (with %s and %s)"LF,tempo,tempo2,liens[liens[ptr]->premier]->fil,liens[ptr]->fil);
  255.             test_flush;
  256.           }
  257.           
  258.           // si vient de primary, ne pas tester lienrelatif avec (car host "diffΘrent")
  259.           /*if (liens[liens[ptr]->premier] == 0) {   // vient de primary
  260.           }
  261.           */
  262.           
  263.           // NEW: finalement OK, sauf pour les moved repΘrΘs par link_import
  264.           // PROBLEME : annulΘ a cause d'un lien Θventuel isolΘ acceptΘ..qui entrainerait un miroir
  265.           
  266.           // (test mΩme niveau (NOUVEAU α cause de certains problΦmes de filtres non intΘgrΘs))
  267.           // NEW
  268.           if ( 
  269.             (tempo[0]  != '\0' && tempo[1]  != '\0' && strchr(tempo+1,'/') == 0)
  270.             ||
  271.             (tempo2[0] != '\0' && tempo2[1] != '\0' && strchr(tempo2+1,'/') == 0) 
  272.             ) {
  273.             if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  274.               forbidden_url=0;
  275.               if ((opt->debug>1) && (opt->log!=NULL)) {
  276.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"same level link authorized: %s%s"LF,adr,fil);
  277.                 test_flush;
  278.              }
  279.             }
  280.           }
  281.           
  282.           // down
  283.           if ( (strncmp(tempo,"../",3)) || (strncmp(tempo2,"../",3)))  {   // pas montΘe sinon ne nbous concerne pas
  284.             int test1,test2;
  285.             if (!strncmp(tempo,"../",3))
  286.               test1=0;
  287.             else
  288.               test1 = (strchr(tempo +((*tempo =='/')?1:0),'/')!=NULL);
  289.             if (!strncmp(tempo2,"../",3))
  290.               test2=0;
  291.             else
  292.               test2 = (strchr(tempo2+((*tempo2=='/')?1:0),'/')!=NULL);
  293.             if ( (test1) && (test2) ) {   // on ne peut que descendre
  294.               if ((opt->seeker & 1)==0) {  // interdiction de descendre
  295.                 forbidden_url=1;
  296.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  297.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"lower link canceled: %s%s"LF,adr,fil);
  298.                   test_flush;
  299.                 }
  300.               } else {    // autorisΘ α priori - NEW
  301.                 if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  302.                   forbidden_url=0;
  303.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  304.                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"lower link authorized: %s%s"LF,adr,fil);
  305.                     test_flush;
  306.                   }
  307.                 }
  308.               }
  309.             } else if ( (test1) || (test2) ) {   // on peut descendre pour accΘder au lien
  310.               if ((opt->seeker & 1)!=0) {  // on peut descendre - NEW
  311.                 if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  312.                   forbidden_url=0;
  313.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  314.                     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"lower link authorized: %s%s"LF,adr,fil);
  315.                     test_flush;
  316.                   }
  317.                 }
  318.               }
  319.             }
  320.           }
  321.           
  322.           
  323.           // up
  324.           if ( (!strncmp(tempo,"../",3)) && (!strncmp(tempo2,"../",3)) ) {    // impossible sans monter
  325.             if ((opt->seeker & 2)==0) {  // interdiction de monter
  326.               forbidden_url=1;
  327.               if ((opt->debug>1) && (opt->log!=NULL)) {
  328.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"upper link canceled: %s%s"LF,adr,fil);
  329.                 test_flush;
  330.               }
  331.             } else {       // autorisΘ α monter - NEW
  332.               if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  333.                 forbidden_url=0;
  334.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  335.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"upper link authorized: %s%s"LF,adr,fil);
  336.                   test_flush;
  337.                 }
  338.               }
  339.             }
  340.           } else if ( (!strncmp(tempo,"../",3)) || (!strncmp(tempo2,"../",3)) ) {    // Possible en montant
  341.             if ((opt->seeker & 2)!=0) {  // autorisΘ α monter - NEW
  342.               if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  343.                 forbidden_url=0;
  344.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  345.                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"upper link authorized: %s%s"LF,adr,fil);
  346.                   test_flush;
  347.                 }
  348.               }
  349.             }  // sinon autorisΘ en descente
  350.           }
  351.           
  352.           
  353.         } else {
  354.           if (opt->log) {
  355.             fprintf(opt->log,"Error building relative link %s and %s"LF,fil,liens[ptr]->fil);
  356.             test_flush;
  357.           }
  358.         }
  359.       } else {
  360.         if (opt->log) {
  361.           fprintf(opt->log,"Error building relative link %s and %s"LF,fil,liens[liens[ptr]->premier]->fil);
  362.           test_flush;
  363.         }
  364.       }
  365.       
  366.     }  // tester interdiction de descendre?
  367.     
  368.     {  // tester interdiction de monter
  369.       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  370.       char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  371.       if (lienrelatif(tempo,fil,liens[liens[ptr]->premier]->fil)==0) {
  372.         if (lienrelatif(tempo2,fil,liens[ptr]->fil)==0) {
  373.         } else {
  374.           if (opt->log) { 
  375.             fprintf(opt->log,"Error building relative link %s and %s"LF,fil,liens[ptr]->fil);
  376.             test_flush;
  377.           }
  378.           
  379.         }
  380.       } else {
  381.         if (opt->log) { 
  382.           fprintf(opt->log,"Error building relative link %s and %s"LF,fil,liens[liens[ptr]->premier]->fil);
  383.           test_flush;
  384.         }
  385.         
  386.       }
  387.     }   // fin tester interdiction de monter
  388.     
  389.   } else {    // adresse diffΘrente, sortir?
  390.     
  391.     //if (!opt->wizard) {    // mode non wizard
  392.     // doit-on traiter ce lien?.. vΘrifier droits de sortie
  393.     switch((opt->travel & 255)) {
  394.     case 0: 
  395.       if (!opt->wizard)    // mode non wizard
  396.         forbidden_url=1; break;    // interdicton de sortir au dela de l'adresse
  397.     case 1: {              // sortie sur le mΩme dom.xxx
  398.       size_t i = strlen(adr)-1;
  399.       size_t j = strlen(urladr)-1;
  400.       while( (i>0) && (adr[i]!='.')) i--;
  401.       while( (j>0) && (urladr[j]!='.')) j--;
  402.       i--; j--;
  403.       while( (i>0) && (adr[i]!='.')) i--;
  404.       while( (j>0) && (urladr[j]!='.')) j--;
  405.       if ((i>0) && (j>0)) {
  406.         if (!strfield2(adr+i,urladr+j)) {   // !=
  407.           if (!opt->wizard) {   // mode non wizard
  408.             //printf("refused: %s\n",adr);
  409.             forbidden_url=1;  // pas mΩme domaine  
  410.             if ((opt->debug>1) && (opt->log!=NULL)) {
  411.               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"foreign domain link canceled: %s%s"LF,adr,fil);
  412.               test_flush;
  413.             }
  414.           }
  415.           
  416.         } else {
  417.           if (opt->wizard) {   // mode wizard
  418.             forbidden_url=0;  // mΩme domaine  
  419.             if ((opt->debug>1) && (opt->log!=NULL)) {
  420.               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"same domain link authorized: %s%s"LF,adr,fil);
  421.               test_flush;
  422.             }
  423.           }
  424.         }
  425.         
  426.       } else
  427.         forbidden_url=1;
  428.             } 
  429.       break;  
  430.     case 2: {                      // sortie sur le mΩme .xxx
  431.       size_t i = strlen(adr)-1;
  432.       size_t j = strlen(urladr)-1;
  433.       while( (i>0) && (adr[i]!='.')) i--;
  434.       while( (j>0) && (urladr[j]!='.')) j--;
  435.       if ((i>0) && (j>0)) {
  436.         if (!strfield2(adr+i,urladr+j)) {   // !-
  437.           if (!opt->wizard) {   // mode non wizard
  438.             //printf("refused: %s\n",adr);
  439.             forbidden_url=1;  // pas mΩme .xx  
  440.             if ((opt->debug>1) && (opt->log!=NULL)) {
  441.               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"foreign location link canceled: %s%s"LF,adr,fil);
  442.               test_flush;
  443.             }
  444.           }
  445.         } else {
  446.           if (opt->wizard) {   // mode wizard
  447.             forbidden_url=0;  // mΩme domaine  
  448.             if ((opt->debug>1) && (opt->log!=NULL)) {
  449.               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"same location link authorized: %s%s"LF,adr,fil);
  450.               test_flush;
  451.             }
  452.           }
  453.         }
  454.       } else forbidden_url=1;     
  455.             } 
  456.       break;
  457.     case 7:                 // everywhere!!
  458.       if (opt->wizard) {   // mode wizard
  459.         forbidden_url=0;
  460.         break;
  461.       }
  462.     }  // switch
  463.     
  464.     // ANCIENNE POS -- rΘcupΘrer les liens α c⌠tΘs d'un lien (nearlink)
  465.     
  466.   }  // fin test adresse identique/diffΘrente
  467.  
  468.   // -------------------- PHASE 3 --------------------
  469.  
  470.   // rΘcupΘrer les liens α c⌠tΘs d'un lien (nearlink) (nvelle pos)
  471.   if (forbidden_url != 0 && opt->nearlink) {
  472.     if (!ishtml(opt,fil)) {  // non html
  473.       //printf("ok %s%s\n",ad,fil);
  474.       forbidden_url=0;    // autoriser
  475.       may_set_prio_to=1+1; // set prio to 1 (parse but skip urls) if near is the winner
  476.       if ((opt->debug>1) && (opt->log!=NULL)) {
  477.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"near link authorized: %s%s"LF,adr,fil);
  478.         test_flush;
  479.       }
  480.     }
  481.   }
  482.  
  483.   // -------------------- PHASE 3-BIS --------------------
  484.  
  485.     /* Built-in known tags (<img src=..>, ..) */
  486.     if (forbidden_url != 0 && embedded_triggered) {
  487.         forbidden_url=0;    // autoriser
  488.         may_set_prio_to=1+1; // set prio to 1 (parse but skip urls) if near is the winner
  489.         if ((opt->debug>1) && (opt->log!=NULL)) {
  490.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"near link authorized (friendly tag): %s%s"LF,adr,fil);
  491.             test_flush;
  492.         }
  493.     }
  494.  
  495.  
  496.   // -------------------- PHASE 4 --------------------
  497.   
  498.   // ------------------------------------------------------
  499.   // Si wizard, il se peut qu'on autorise ou qu'on interdise 
  500.   // un lien spΘcial avant mΩme de tester sa position, sa hiΘrarchie etc.
  501.   // peut court-circuiter le forbidden_url prΘcΘdent
  502.   if (opt->wizard) { // le wizard entre en action..
  503.     //
  504.     int question=1;         // poser une question                            
  505.     int force_mirror=0;     // pour mirror links
  506.     int filters_answer=0;   // dΘcision prise par les filtres
  507.     char BIGSTK l[HTS_URLMAXSIZE*2];
  508.     char BIGSTK lfull[HTS_URLMAXSIZE*2];
  509.     
  510.     if (forbidden_url!=-1) question=0;  // pas de question, rΘsolu
  511.     
  512.     // former URL complΦte du lien actuel
  513.     strcpybuff(l,jump_identification(adr));
  514.     if (*fil!='/') strcatbuff(l,"/");
  515.     strcatbuff(l,fil);
  516.     // full version (http://foo:bar@www.foo.com/bar.html)
  517.     if (!link_has_authority(adr))
  518.       strcpybuff(lfull,"http://");
  519.     else
  520.       lfull[0]='\0';
  521.     strcatbuff(lfull,adr);
  522.     if (*fil!='/') strcatbuff(lfull,"/");
  523.     strcatbuff(lfull,fil);
  524.     
  525.     // tester filters (URLs autorisΘes ou interdites explicitement)
  526.     
  527.     // si lien primaire on saute le joker, on est pas lΘmur
  528.     if (ptr==0) {  // lien primaire, autoriser
  529.       question=1;    // la question sera rΘsolue automatiquement
  530.       forbidden_url=0;
  531.       may_set_prio_to=0;    // clear may-set flag
  532.     } else {
  533.       // eternal depth first
  534.       // vΘrifier rΘcursivitΘ extΘrieure
  535.       if (opt->extdepth>0) {
  536.         if ( /*question && */ (ptr>0) && (!force_mirror)) {
  537.           // well, this is kinda a hak
  538.           // we don't want to mirror EVERYTHING, and we have to decide where to stop
  539.           // there is no way yet to tag "external" links, and therefore links that are
  540.           // "weak" (authorized depth < external depth) are just not considered for external
  541.           // hack
  542.           if (liens[ptr]->depth > opt->extdepth) {
  543.             // *set_prio_to = opt->extdepth + 1;
  544.             *set_prio_to = 1 + (opt->extdepth);
  545.             may_set_prio_to=0;  // clear may-set flag
  546.             forbidden_url=0;    // autorisΘ
  547.             question=0;         // rΘsolution auto
  548.             if ((opt->debug>1) && (opt->log!=NULL)) {
  549.               if (question) {
  550.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) ambiguous link accepted (external depth): link %s at %s%s"LF,l,urladr,urlfil);
  551.               } else {
  552.                 HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) forced to accept link (external depth): link %s at %s%s"LF,l,urladr,urlfil);
  553.               }
  554.               test_flush;
  555.             }
  556.             
  557.           }
  558.         }
  559.       }  
  560.       
  561.       // filters
  562.       {
  563.         int jok;
  564.         char* mdepth="";
  565.         // filters, 0=sait pas 1=ok -1=interdit
  566.         {
  567.           int jokDepth1=0,jokDepth2=0;
  568.           int jok1=0,jok2=0;
  569.           jok1  = fa_strjoker(/*url*/0, _FILTERS,*_FILTERS_PTR,lfull,NULL,NULL,&jokDepth1);
  570.           jok2 =  fa_strjoker(/*url*/0, _FILTERS,*_FILTERS_PTR,l,    NULL,NULL,&jokDepth2);
  571.           if (jok2 == 0) {      // #2 doesn't know
  572.             jok = jok1;        // then, use #1
  573.             mdepth = _FILTERS[jokDepth1];
  574.           } else if (jok1 == 0) { // #1 doesn't know
  575.             jok = jok2;        // then, use #2
  576.             mdepth = _FILTERS[jokDepth2];
  577.           } else if (jokDepth1 >= jokDepth2) { // #1 matching rule is "after" #2, then it is prioritary
  578.             jok = jok1;
  579.             mdepth = _FILTERS[jokDepth1];
  580.           } else {                             // #2 matching rule is "after" #1, then it is prioritary
  581.             jok = jok2;
  582.             mdepth = _FILTERS[jokDepth2];
  583.           }
  584.         }
  585.         
  586.         if (jok == 1) {   // autorisΘ
  587.           filters_answer=1;  // dΘcision prise par les filtres
  588.           question=0;    // ne pas poser de question, autorisΘ
  589.           forbidden_url=0;  // URL autorisΘe
  590.           may_set_prio_to=0;    // clear may-set flag
  591.           if ((opt->debug>1) && (opt->log!=NULL)) {
  592.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) explicit authorized (%s) link: link %s at %s%s"LF,mdepth,l,urladr,urlfil);
  593.             test_flush;
  594.           }
  595.         } else if (jok == -1) {  // forbidden
  596.           filters_answer=1;  // dΘcision prise par les filtres
  597.           question=0;    // ne pas poser de question:
  598.           forbidden_url=1;   // URL interdite
  599.           if ((opt->debug>1) && (opt->log!=NULL)) {
  600.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) explicit forbidden (%s) link: link %s at %s%s"LF,mdepth,l,urladr,urlfil);
  601.             test_flush;
  602.           }
  603.         }  // sinon on touche α rien
  604.       }
  605.     }
  606.     
  607.     // vΘrifier mode mirror links
  608.     if (question) {
  609.       if (opt->mirror_first_page) {    // mode mirror links
  610.         if (liens[ptr]->precedent==0) {  // parent=primary!
  611.           forbidden_url=0;    // autorisΘ
  612.           may_set_prio_to=0;    // clear may-set flag
  613.           question=1;         // rΘsolution auto
  614.           force_mirror=5;     // mirror (5)
  615.           if ((opt->debug>1) && (opt->log!=NULL)) {
  616.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) explicit mirror link: link %s at %s%s"LF,l,urladr,urlfil);
  617.             test_flush;
  618.           }
  619.         }
  620.       }
  621.     }
  622.     
  623.     // on doit poser la question.. peut on la poser?
  624.     // (oui je sais quel preuve de dΘlicatesse, merci merci)      
  625.     if ((question) && (ptr>0) && (!force_mirror)) {
  626.       if (opt->wizard==2) {    // Θliminer tous les liens non rΘpertoriΘs comme autorisΘs (ou inconnus)
  627.         question=0;
  628.         forbidden_url=1;
  629.         if ((opt->debug>1) && (opt->log!=NULL)) {
  630.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) ambiguous forbidden link: link %s at %s%s"LF,l,urladr,urlfil);
  631.           test_flush;
  632.         }
  633.       }
  634.     }
  635.     
  636.     // vΘrifier robots.txt
  637.     if (opt->robots) {
  638.       int r = checkrobots(_ROBOTS,adr,fil);
  639.       if (r == -1) {    // interdiction
  640. #if DEBUG_ROBOTS
  641.         printf("robots.txt forbidden: %s%s\n",adr,fil);
  642. #endif
  643.         // question rΘsolue, par les filtres, et mode robot non strict
  644.         if ((!question) && (filters_answer) && (opt->robots == 1) && (forbidden_url!=1)) {
  645.           r=0;    // annuler interdiction des robots
  646.           if (!forbidden_url) {
  647.             if ((opt->debug>1) && (opt->log!=NULL)) {
  648.               HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Warning link followed against robots.txt: link %s at %s%s"LF,l,adr,fil);
  649.               test_flush;
  650.             }
  651.           }
  652.         }
  653.         if (r == -1) {    // interdire
  654.           forbidden_url=1;
  655.           question=0;
  656.           if ((opt->debug>1) && (opt->log!=NULL)) {
  657.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(robots.txt) forbidden link: link %s at %s%s"LF,l,adr,fil);
  658.             test_flush;
  659.           }
  660.         }
  661.       }
  662.     }
  663.     
  664.     if (!question) {
  665.       if ((opt->debug>1) && (opt->log!=NULL)) {
  666.         if (!forbidden_url) {
  667.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) shared foreign domain link: link %s at %s%s"LF,l,urladr,urlfil);
  668.         } else {
  669.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) cancelled foreign domain link: link %s at %s%s"LF,l,urladr,urlfil);
  670.         }
  671.         test_flush;
  672.       }
  673. #if BDEBUG==3
  674.       printf("at %s in %s, wizard says: url %s ",urladr,urlfil,l);
  675.       if (forbidden_url) printf("cancelled"); else printf(">SHARED<");
  676.       printf("\n");
  677. #endif 
  678.     }
  679.  
  680.     /* en cas de question, ou lien primaire (enregistrer autorisations) */
  681.     if (question || (ptr==0)) {
  682.       const char* s;
  683.       int n=0;
  684.       
  685.       // si primaire (plus bas) alors ...
  686.       if ((ptr!=0) && (force_mirror==0)) {
  687.         HTS_REQUEST_START;
  688.         HT_PRINT("\n");
  689.         HT_PRINT("At "); HT_PRINT(urladr); HT_PRINT(", there is a link ("); HT_PRINT(adr); HT_PRINT("/"); HT_PRINT(fil); HT_PRINT(") which goes outside the address."LF);
  690.         HT_PRINT("What should I do? (press a key + enter)"LF LF);
  691.         HT_PRINT("* Ignore all further links" LF);
  692.         HT_PRINT("0 Ignore this link (default if empty entry)"LF);
  693.         HT_PRINT("1 Ignore directory and lower structures"LF);
  694.         HT_PRINT("2 Ignore all domain"LF);
  695.         //HT_PRINT("3 (Ignore location, not implemented)\n");
  696.         HT_PRINT(LF);
  697.         HT_PRINT("4 Get only this page/link"LF);
  698.         HT_PRINT("5 Mirror this link (useful)"LF);
  699.         HT_PRINT("6 Mirror links located in the same domain"LF);
  700.         HT_PRINT(LF);
  701.         HTS_REQUEST_END;
  702.           {
  703.             char BIGSTK tempo[HTS_URLMAXSIZE*2];
  704.             tempo[0]='\0';
  705.             strcatbuff(tempo,adr);
  706.             strcatbuff(tempo,"/");
  707.             strcatbuff(tempo,fil);
  708.             s = RUN_CALLBACK1(opt, query3, tempo);
  709.           }
  710.           if (strnotempty(s)==0)  // entrΘe
  711.             n=0;
  712.           else if (isdigit((unsigned char)*s))
  713.             sscanf(s,"%d",&n);
  714.           else {
  715.             switch(*s) {
  716.             case '*': n=-1; break;
  717.             case '!': n=-999; {
  718.               /*char *a;
  719.               int i;                                    
  720.               a=copie_de_adr-128;
  721.               if (a<r.adr) a=r.adr;
  722.               for(i=0;i<256;i++) {
  723.                 if (a==copie_de_adr) printf("\nHERE:\n");
  724.                 printf("%c",*a++);
  725.               }
  726.               printf("\n\n");
  727.               */
  728.                       }
  729.               break;
  730.             default: n=-999; printf("What did you say?\n"); break;
  731.               
  732.             } 
  733.           }
  734.         io_flush;
  735.       } else {   // lien primaire: autoriser rΘpertoire entier       
  736.         if (!force_mirror) {
  737.           if ((opt->seeker & 1)==0) {  // interdiction de descendre
  738.             n=7;
  739.           } else {
  740.             n=5;   // autoriser miroir rΘpertoires descendants (lien primaire)
  741.           }
  742.         } else   // forcer valeur (sub-wizard)
  743.           n=force_mirror;
  744.       }
  745.       
  746.       /* sanity check - reallocate filters HERE */
  747.       if ((*_FILTERS_PTR) + 1 >= opt->maxfilter) {
  748.         opt->maxfilter += HTS_FILTERSINC;
  749.         if (filters_init(&_FILTERS, opt->maxfilter, HTS_FILTERSINC) == 0) {
  750.           printf("PANIC! : Too many filters : >%d [%d]\n", (*_FILTERS_PTR),__LINE__);
  751.           fflush(stdout);
  752.           if (opt->log) {
  753.             fprintf(opt->log,LF"Too many filters, giving up..(>%d)"LF, (*_FILTERS_PTR) );
  754.             fprintf(opt->log,"To avoid that: use #F option for more filters (example: -#F5000)"LF);
  755.             test_flush;
  756.           }
  757.           assertf("too many filters - giving up" == NULL);    // wild..
  758.         }
  759.       }
  760.  
  761.       // here we have enough room for a new filter if necessary
  762.       switch(n) {
  763.       case -1: // sauter tout le reste
  764.         forbidden_url=1;
  765.         opt->wizard=2;    // sauter tout le reste
  766.         break;
  767.       case 0:    // interdire les mΩmes liens: adr/fil
  768.         forbidden_url=1; 
  769.         HT_INSERT_FILTERS0;    // insΘrer en 0
  770.         strcpybuff(_FILTERS[0],"-");
  771.         strcatbuff(_FILTERS[0],jump_identification(adr));
  772.         if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  773.         strcatbuff(_FILTERS[0],fil);
  774.         break;
  775.         
  776.       case 1: // Θliminer rΘpertoire entier et sous rΘp: adr/path/ *
  777.         forbidden_url=1;
  778.         {
  779.           size_t i = strlen(fil)-1;
  780.           while((fil[i]!='/') && (i>0)) i--;
  781.           if (fil[i]=='/') {
  782.             HT_INSERT_FILTERS0;    // insΘrer en 0
  783.             strcpybuff(_FILTERS[0],"-");
  784.             strcatbuff(_FILTERS[0],jump_identification(adr));
  785.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  786.             strncatbuff(_FILTERS[0] ,fil,i);
  787.             if (_FILTERS[0][strlen(_FILTERS[0])-1]!='/') 
  788.               strcatbuff(_FILTERS[0],"/");
  789.             strcatbuff(_FILTERS[0],"*");
  790.           }
  791.         }            
  792.         
  793.         // ** ...
  794.         break;
  795.         
  796.       case 2:    // adresse adr*
  797.         forbidden_url=1;
  798.         HT_INSERT_FILTERS0;    // insΘrer en 0                                
  799.         strcpybuff(_FILTERS[0],"-");
  800.         strcatbuff(_FILTERS[0],jump_identification(adr));
  801.         strcatbuff(_FILTERS[0],"*");
  802.         break;
  803.         
  804.       case 3: // ** A FAIRE
  805.         forbidden_url=1;
  806.         /*
  807.         {
  808.         int i=strlen(adr)-1;
  809.         while((adr[i]!='/') && (i>0)) i--;
  810.         if (i>0) {
  811.         
  812.           }
  813.           
  814.       }*/
  815.         
  816.         break;
  817.         //
  818.       case 4:    // same link
  819.         // PAS BESOIN!!
  820.         /*HT_INSERT_FILTERS0;    // insΘrer en 0                                
  821.         strcpybuff(_FILTERS[0],"+");
  822.         strcatbuff(_FILTERS[0],adr);
  823.         if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  824.         strcatbuff(_FILTERS[0],fil);*/
  825.         
  826.         
  827.         // Θtant donnΘ le renversement wizard/primary filter (les primary autorisent up/down ET interdisent)
  828.         // il faut Θviter d'un lien isolΘ effectue un miroir total..
  829.         
  830.         *set_prio_to = 0+1;    // niveau de rΘcursion=0 (pas de miroir)
  831.         
  832.         break;
  833.         
  834.       case 5:    // autoriser rΘpertoire entier et fils
  835.         if ((opt->seeker & 2)==0) {  // interdiction de monter
  836.           size_t i = strlen(fil)-1;
  837.           while((fil[i]!='/') && (i>0)) i--;
  838.           if (fil[i]=='/') {
  839.             HT_INSERT_FILTERS0;    // insΘrer en 0                                
  840.             strcpybuff(_FILTERS[0],"+");
  841.             strcatbuff(_FILTERS[0],jump_identification(adr));
  842.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  843.             strncatbuff(_FILTERS[0],fil,i+1);
  844.             strcatbuff(_FILTERS[0],"*");
  845.           }
  846.         } else {    // autoriser domaine alors!!
  847.           HT_INSERT_FILTERS0;    // insΘrer en 0                                strcpybuff(filters[filptr],"+");
  848.           strcpybuff(_FILTERS[0],"+");
  849.           strcatbuff(_FILTERS[0],jump_identification(adr));
  850.           strcatbuff(_FILTERS[0],"*");
  851.         }
  852.         break;
  853.         
  854.       case 6:    // same domain
  855.         HT_INSERT_FILTERS0;    // insΘrer en 0                                strcpybuff(filters[filptr],"+");
  856.         strcpybuff(_FILTERS[0],"+");
  857.         strcatbuff(_FILTERS[0],jump_identification(adr));
  858.         strcatbuff(_FILTERS[0],"*");
  859.         break;
  860.         //
  861.       case 7:    // autoriser ce rΘpertoire
  862.         {
  863.           size_t i = strlen(fil)-1;
  864.           while((fil[i]!='/') && (i>0)) i--;
  865.           if (fil[i]=='/') {
  866.             HT_INSERT_FILTERS0;    // insΘrer en 0                                
  867.             strcpybuff(_FILTERS[0],"+");
  868.             strcatbuff(_FILTERS[0],jump_identification(adr));
  869.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  870.             strncatbuff(_FILTERS[0],fil,i+1);
  871.             strcatbuff(_FILTERS[0],"*[file]");
  872.           }
  873.         }
  874.         
  875.         break;
  876.         
  877.       case 50:    // on fait rien
  878.         break;
  879.       }  // switch 
  880.                               
  881.     }  // test du wizard sur l'url
  882.   }  // fin du test wizard..
  883.  
  884.   // -------------------- PHASE 5 --------------------
  885.  
  886.   // lien non autorisΘ, peut-on juste le tester?
  887.   if (just_test_it) {
  888.     if (forbidden_url==1) {
  889.       if (opt->travel&256) {    // tester tout de mΩme
  890.         if (strfield(adr,"ftp://")==0
  891. #if HTS_USEMMS
  892.                     && strfield(adr,"mms://")==0
  893. #endif
  894.                     ) {    // PAS ftp!
  895.           forbidden_url=1;    // oui oui toujours interdit (note: sert α rien car ==1 mais c pour comprendre)
  896.           *just_test_it=1;     // mais on teste
  897.           if ((opt->debug>1) && (opt->log!=NULL)) {
  898.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link %s%s"LF,adr,fil);
  899.           }
  900.         }
  901.       }
  902.     }
  903.     //adr[0]='\0';  // cancel
  904.   }
  905.  
  906.   // -------------------- FINAL PHASE --------------------
  907.   // Test if the "Near" test won
  908.   if (may_set_prio_to && forbidden_url == 0) {
  909.     *set_prio_to = may_set_prio_to;
  910.   }
  911.  
  912.   return forbidden_url;
  913. #undef _FILTERS
  914. #undef _FILTERS_PTR
  915. #undef _ROBOTS
  916. }
  917.  
  918. int hts_acceptmime(httrackp* opt,
  919.                    int ptr,int lien_tot,lien_url** liens,
  920.                    char* adr,char* fil,
  921.                    char* mime) 
  922. {
  923. #define _FILTERS     (*opt->filters.filters)
  924. #define _FILTERS_PTR (opt->filters.filptr)
  925. #define _ROBOTS      ((robots_wizard*)opt->robotsptr)
  926.   int forbidden_url = -1;
  927.   char* mdepth="";
  928.   int jokDepth = 0;
  929.   int jok = 0;
  930.  
  931.   /* Authorized ? */
  932.   jok  = fa_strjoker(/*mime*/1, _FILTERS, *_FILTERS_PTR, mime, NULL, NULL, &jokDepth);
  933.   if (jok != 0) {
  934.     mdepth = _FILTERS[jokDepth];
  935.     if (jok == 1) {   // autorisΘ
  936.       forbidden_url=0;  // URL autorisΘe
  937.       if ((opt->debug>1) && (opt->log!=NULL)) {
  938.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) explicit authorized (%s) link %s%s: mime '%s'"LF,mdepth,adr,fil,mime);
  939.         test_flush;
  940.       }
  941.     } else if (jok == -1) {  // forbidden
  942.       forbidden_url=1;   // URL interdite
  943.       if ((opt->debug>1) && (opt->log!=NULL)) {
  944.         HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(wizard) explicit forbidden (%s) link %s%s: mime '%s'"LF,mdepth,adr,fil,mime);
  945.         test_flush;
  946.       }
  947.     }  // sinon on touche α rien
  948.   }
  949.   /* userdef test */
  950.     {
  951.     int test_url = RUN_CALLBACK4(opt, check_mime, adr, fil, mime, forbidden_url);
  952.     if (test_url!=-1) {
  953.       forbidden_url=test_url;
  954.     }
  955.     }
  956.   return forbidden_url;
  957. #undef _FILTERS
  958. #undef _FILTERS_PTR
  959. #undef _ROBOTS
  960. }
  961.  
  962. // tester taille
  963. int hts_testlinksize(httrackp* opt,
  964.                      char* adr,char* fil,
  965.                      LLint size) {
  966.   int jok=0;
  967.   if (size>=0) {
  968.     char BIGSTK l[HTS_URLMAXSIZE*2];
  969.     char BIGSTK lfull[HTS_URLMAXSIZE*2];
  970.     if (size>=0) {
  971.       LLint sz=size;
  972.       int size_flag=0;
  973.       
  974.       // former URL complΦte du lien actuel
  975.       strcpybuff(l,jump_identification(adr));
  976.       if (*fil!='/') strcatbuff(l,"/");
  977.       strcatbuff(l,fil);
  978.       //
  979.       if (!link_has_authority(adr))
  980.         strcpybuff(lfull,"http://");
  981.       else
  982.         lfull[0]='\0';
  983.       strcatbuff(lfull,adr);
  984.       if (*fil!='/') strcatbuff(l,"/");
  985.       strcatbuff(lfull,fil);
  986.       
  987.       // filters, 0=sait pas 1=ok -1=interdit
  988.       {
  989.         int jokDepth1=0,jokDepth2=0;
  990.         int jok1=0,jok2=0;
  991.         LLint sz1=size,sz2=size;
  992.         int size_flag1=0,size_flag2=0;
  993.         jok1  = fa_strjoker(/*url*/0, *opt->filters.filters,*opt->filters.filptr,lfull,&sz1,&size_flag1,&jokDepth1);
  994.         jok2 =  fa_strjoker(/*url*/0, *opt->filters.filters,*opt->filters.filptr,l,    &sz2,&size_flag2,&jokDepth2);
  995.         if (jok2 == 0) {      // #2 doesn't know
  996.           jok = jok1;        // then, use #1
  997.           sz = sz1;
  998.           size_flag = size_flag1;
  999.         } else if (jok1 == 0) {  // #1 doesn't know
  1000.           jok = jok2;        // then, use #2
  1001.           sz = sz2;
  1002.           size_flag = size_flag2;
  1003.         } else if (jokDepth1 >= jokDepth2) { // #1 matching rule is "after" #2, then it is prioritary
  1004.           jok = jok1;
  1005.           sz = sz1;
  1006.           size_flag = size_flag1;
  1007.         } else {                              // #2 matching rule is "after" #1, then it is prioritary
  1008.           jok = jok2;
  1009.           sz = sz2;
  1010.           size_flag = size_flag2;
  1011.         } 
  1012.       }
  1013.       
  1014.  
  1015.       // log
  1016.       if (jok==1) {
  1017.         if ((opt->debug>1) && (opt->log!=NULL)) {
  1018.           HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File confirmed (size test): %s%s ("LLintP")"LF,adr,fil,(LLint)(size));
  1019.         }
  1020.       } else if (jok==-1) {
  1021.         if (size_flag) {        /* interdit α cause de la taille */
  1022.           if ((opt->debug>1) && (opt->log!=NULL)) {
  1023.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File cancelled due to its size: %s%s ("LLintP", limit: "LLintP")"LF,adr,fil,(LLint)(size),(LLint)(sz));
  1024.           }
  1025.         } else {
  1026.           jok=1;
  1027.         }
  1028.       }
  1029.     }
  1030.   }
  1031.   return jok;
  1032. }
  1033.  
  1034.  
  1035.  
  1036. #undef test_flush
  1037. #undef urladr
  1038. #undef urlfil
  1039.  
  1040. #undef HT_INSERT_FILTERS0
  1041.  
  1042.